<template>
  <!-- 设备类型新增 -->
  <el-dialog
    v-dialogDrag
    :close-on-click-modal="false"
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    :before-close="cancel"
    class="dialogStyleSmall"
  >
    <div style="text-align:left">
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级类型" prop="parentId">
              <treeselect
                v-model="formData.parentId"
                :show-count="false"
                :options="equOptions"
                placeholder="选择上级类型"
                no-options-text="暂无类型"
                clearable
                @select="selectParentId"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型名称" prop="typeName">
              <el-input v-model="formData.typeName" size="medium" />
            </el-form-item>
          </el-col>
          <!--          <el-col :span="24">-->
          <!--            <el-form-item label="类型编号" prop="typeNum">-->
          <!--              <el-input v-model="formData.typeNum" size="medium" />-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="类型排序" prop="orderNum">
              <el-input-number v-model="formData.orderNum" size="medium" style="width: 100%" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="" prop="remark">
              <label slot="label" v-html="`备&emsp;&emsp;注`" />
              <el-input v-model="formData.remark" :rows="3" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button size="medium" :disabled="btnDisabled" type="primary" @click="submitForm('form')">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addProspectType,
  editProspectType,
  detailProspectType,
  getMaxNum,
  treeProspectType
} from '@/api/superviseBaseInfo/typeManage/equipmentType'
// 菜单下拉树用的的方法和样式
import treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'AddDialog',
  components: { treeselect },
  data() {
    return {
      /* 新增弹框 start */
      // 弹出的对话框的标题
      btnDisabled: false,
      dialogTitle: '',
      // 对话框是否弹出显示
      dialogVisible: false,
      // 类型名称树
      equOptions: [],
      // 添加字段定义
      formData: {
        id: '',
        parentId: null, // 父级id
        typeName: '', // 类型名称
        typeNum: '', // 类型编号
        level: '', // 等级
        orderNum: '', // 类型排序
        remark: '' // 备注
      },
      rules: {
        typeName: [
          { required: true, message: '类型名称不能为空', trigger: 'blur' }
        ],
        typeNum: [
          { required: true, message: '类型编号不能为空', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '类型等级不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '类型排序不能为空', trigger: 'blur' }
        ]
      }
      /* 新增弹框 end */
    }
  },
  methods: {
    /* 新增 start */
    handleAdd(title, id, multip) {
      this.dialogVisible = true
      this.dialogTitle = title
      this.getTreeMenu()
      this.emptyForm()
      console.log(id)
      if (id) {
        this.getDetailById(id)
      } else {
        const { parentId, id } = multip
        this.formData.parentId = id // 新增父级回显
        const numId = !parentId ? 0 : id // 查询排序的父级id
        this.getMaxNum(numId)
      }
    },
    async getTreeMenu() {
      const { data } = await treeProspectType()
      this.equOptions = data
    },
    selectParentId(node) {
      this.formData.parentId = node.id
      this.getMaxNum(node.id)
    },
    // 获取最大排序
    async getMaxNum(parentId) {
      console.log(parentId)
      const { data } = await getMaxNum({ parentId })
      this.formData.orderNum = data
    },
    // 获取详情
    async getDetailById(id) {
      console.log(id)
      const { data } = await detailProspectType(id)
      this.formData = { ...data }
      if (data.parentId === '0') {
        this.formData.parentId = null
      }
    },
    // 添加弹框取消
    cancel() {
      this.dialogVisible = false
      this.btnDisabled = false
    },
    // 添加/编辑页面确定按钮
    submitForm(formData) {
      this.$refs[formData].validate(async valid => {
        if (valid) {
          this.btnDisabled = true
          if (this.formData.id) { // 编辑
            await editProspectType(this.formData)
            this.msgSuccess('修改成功!')
          } else { // 新增
            await addProspectType(this.formData)
            this.msgSuccess('新增成功!')
          }
          this.cancel()
          this.$parent.getList()
        }
      })
    },
    // 重置表单
    emptyForm() {
      this.resetForm('form')
      this.formData.id = ''
    }
    /* 新增 end */
  }
}
</script>

